package com.rocket.consumer.controller;

import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description:
 * @ClassName: UserController
 * @Author: mobiusband
 * @Date:2022/11/16 22:10
 * @Version: 1.0
 */
@RestController
//监听 user
@RocketMQMessageListener(topic = "user1", consumerGroup = "my-consumer-group")
public class UserController implements RocketMQListener<String> {
    String user;
//用户信息redis存储key
    String key = "user2";

    @Resource
    RedisTemplate redisTemplate;

    @Override
    public void onMessage(String s) {
//        如果有信息则传入redis
        if ( s!= null){
            user = s;
            redisTemplate.opsForValue().set(key, s);
        }
    }
// 读取redis内容， 若存在，则返回用户信息，否则提示没有注册
    @GetMapping("/getUser")
    public String getUser(){
        Object o = redisTemplate.opsForValue().get(key);
        String s = o + "";
        if (!"".equals(s)){
            return "用户信息为:"+s;
        }
        return "用户还没注册";
    }
}
